package cn.utum.sungrowplant.common.result;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = -9141307870732228707L;
    
    public static final String SUCCESS = "success";
    public static final String ERROR = "error";
    
    private String state;
    private String msg;
    private T obj;
    
    public R(T obj){
        super();
        this.state = SUCCESS;
        this.obj = obj;
    }

    public R<T> success(T obj){
        this.state = SUCCESS;
        this.obj = obj;
        return this;
    }
    
    public R<T> error(String msg){
        this.state = ERROR;
        this.msg = msg;
        return this;
    }

    @JSONField(serialize = false)
    @JsonIgnore
    public Boolean getIsSuccess() {
        return SUCCESS.equals(this.state);
    }
    
    public String toJson() {
        return JSON.toJSONString(this);
    }
}
